package pcloud.client.watcher;

import net.contentobjects.jnotify.JNotify;
import net.contentobjects.jnotify.JNotifyException;
import net.contentobjects.jnotify.JNotifyListener;
import pcloud.client.scheduler.CloudEvent;
import pcloud.client.scheduler.EventMonitorInterface;
import pcloud.client.util.FileResourceInfo;
import pcloud.client.util.Logger;
import pcloud.client.util.TreeDB;

public class OSClientWatcher implements ClientWatcher, JNotifyListener
{
    private String watchPath = "D:\\watch";

    private EventMonitorInterface monitor;
    
    private int watchID;

    public OSClientWatcher()
    {
    }
    
    public OSClientWatcher(EventMonitorInterface monitor)
    {
        this.monitor = monitor;
    }

	public void fileRenamed(int wd, String rootPath, String oldName,
			String newName)
	{
		print("renamed " + rootPath + " : " + oldName + " -> " + newName);
	}

	public void fileModified(int wd, String rootPath, String name)
	{
		print("modified " + rootPath + " : " + name);
		String fullPath = rootPath + name;
		logger.info("File Modified:" + fullPath);
        this.monitor.raiseEvent(new CloudEvent(
            CloudEvent.Side.Client, CloudEvent.Entity.File, CloudEvent.Action.Modify, fullPath));
	}

	public void fileDeleted(int wd, String rootPath, String name)
	{
		print("deleted " + rootPath + " : " + name);
		String fullPath = rootPath + name;
		logger.info("File Deleted:" + rootPath);
        this.monitor.raiseEvent(new CloudEvent(
            CloudEvent.Side.Client, CloudEvent.Entity.File, CloudEvent.Action.Delete, fullPath));

	}

	public void fileCreated(int wd, String rootPath, String name)
	{
		print("created " + rootPath + " : " + name);
		String fullPath = rootPath + name;
		logger.info("File created:" + fullPath);
        this.monitor.raiseEvent(new CloudEvent(
            CloudEvent.Side.Client, CloudEvent.Entity.File, CloudEvent.Action.Create, fullPath));
	}

	void print(String msg)
	{
		System.err.println(msg);
	}
	
    public void start()
    {
    	this.monitor = monitor;
		logger.info("client watcher starting");
//        String path = System.getProperty("user.home");

        // watch mask, specify events you care about,
        // or JNotify.FILE_ANY for all events.
        int mask = JNotify.FILE_CREATED  | 
                   JNotify.FILE_DELETED  | 
                   JNotify.FILE_MODIFIED | 
                   JNotify.FILE_RENAMED;

        // watch subtree?
        boolean watchSubtree = true;

        // add actual watch
        try {
			watchID = JNotify.addWatch(watchPath, mask, watchSubtree, this);
		} catch (JNotifyException e) {
			logger.info("start watcher failed: "+e);
		}

    }

    public void stop()
    {
		logger.info("client watcher ending");
        boolean res;
		try {
			res = JNotify.removeWatch(watchID);
	        if (!res) {
	        	logger.info("invalid watch ID specified.");
	        }
		} catch (JNotifyException e) {
			logger.info("stop watcher failed: "+e);
		}
    }

	@Override
	public void setLocalDB(TreeDB<FileResourceInfo> localDB)
	{
		// TODO Auto-generated method stub
		
	}

}
